﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GameSchool.Models;

namespace GameSchool.Controllers
{
    public class CommentController : Controller
    {
		VideoRepository videoRep = new VideoRepository();
		GameRepository grep = new GameRepository();
		CommentRepository cRep = new CommentRepository();

        public ActionResult Index()
        {
			if (User.IsInRole("Admin"))
			{
				var items = from n in cRep.Comments()
							orderby n.ID ascending
							select n;
				return View(items);
			}
			else
			{
				return RedirectToAction("Index", "Home");
			}
        }

		public ActionResult CreateComment(int? id)
		{
			if (id.HasValue)
			{
				ViewData["currVideo"] = id;
				var nafn = ((from n in videoRep.Videos()
							 where n.ID == id
							 select n).SingleOrDefault()).Title;
				ViewData["videoName"] = nafn;
				//TempData["currUrl"] = ("/Video/ViewVideo/" + id).ToString();
				return View(new Comment());
			}
			else
			{
				return RedirectToAction("ViewVideo", "Video");
			}
			
		}

		[HttpPost]
		//[Authorize(Roles = "Admin, Teacher")]
		public ActionResult createComment(FormCollection formData)
		{
				Comment newComment = new Comment();
				newComment.Date = DateTime.Now;
				cRep.Add(newComment);
				UpdateModel(newComment);
				//setja inn validation
				cRep.Save();
				return Redirect("../.." + TempData["currUrl"]);
		}
    }
}
